//
//  BottomMenu.swift
//  iPad_QQ
//
//  Created by 李善武 on 16/4/29.
//  Copyright © 2016年 李善武. All rights reserved.
//

import UIKit
@objc enum menuItemType : Int{
    case Mood
    case Photo
    case Blog
}
@objc protocol menuItemDelegate : NSObjectProtocol{

    optional func menuItemBtn(menu : BottomMenu ,menuType : menuItemType)
}
class BottomMenu: UIView {
   
    var delegate : menuItemDelegate?
    
       // 重写init方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 初始化子控件
        setItems("tabbar_mood" ,type: .Mood)
        setItems("tabbar_photo" ,type: .Photo)
        setItems("tabbar_blog" ,type: .Blog)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
      // 让外界告知当前的屏幕方向
       func rotateToLandscape(isLandscape : Bool){
        // 得到子控件的个数
        let count  = subviews.count
        
        // 设置自身的frame
        frame.size.width = (superview?.frame.width)!
        frame.size.height = isLandscape ? dockItemHeight :dockItemHeight*CGFloat(count)
        frame.origin.y = (superview?.frame.height)! - frame.size.height
        
        for var i=0 ; i < count ; i++ {
            let btn = subviews[i]
            btn.frame.size.width = isLandscape ?(frame.size.width)/CGFloat(count) : frame.size.width
            btn.frame.size.height = isLandscape ? dockItemHeight :dockItemHeight
            btn.frame.origin.x = isLandscape ? btn
                .frame.size.width*CGFloat(i) : 0
            btn.frame.origin.y = isLandscape ? 0 :btn.frame.size.height*CGFloat(i)
        }
    }
}


// setItems方法
extension BottomMenu {

    private func setItems(imageName : String ,type : menuItemType){
        
          // 创建按钮
          let item = UIButton()
          addSubview(item)
          item.tag = type.rawValue
          // 设置图片
          item.setImage(UIImage(named: imageName), forState: .Normal)
          item.setBackgroundImage(UIImage(named: "tabbar_separate_selected_bg"), forState: .Highlighted)
        
          // menu点击事件
          item.addTarget(self, action: "menuItemClick:", forControlEvents: .TouchUpInside)
    }
}

// 按钮点击事件
extension BottomMenu{
    @objc func menuItemClick(menuItem : UIButton){
        delegate?.menuItemBtn!(self, menuType: menuItemType(rawValue: menuItem.tag)!)
    }
}
